<?php
namespace app\business\controller;
use app\business\model\BusinessClassify;
use app\business\model\BusinessLogin;
use app\business\model\BusinessMenu;
use think\Controller;
use app\business\controller\Base;
use think\Session;

class Menu extends Base
{
    public function index()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //获取到当前商家的菜品

        $data = BusinessMenu::where('menu_delete',1)->order(['menu_order' => 'desc'])->where('business_id',$num)->paginate(10);

        $this->assign('data',$data);

        return $this->fetch();
    }

    public function create()
    {
        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id']; //获取到当前商家的菜品
        $data = BusinessClassify::where('business_id',$num)->where('classify_delete',1)->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function store()
    {

        //var_dump($_POST);die;
        $_POST['menu_classify'] = $_POST['classify_name'];

        unset($_POST['classify_name']);

        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id'];

        $_POST['business_id'] = $num;

        $file = request()->file("menu_img");

        $info = $file->move(ROOT_PATH . 'public/uploads/shop');

        $nowDate=date('Ymd');

        $path=$nowDate.'/'.$info->getFilename();

        $_POST['menu_img'] = $path;

        $username = Session::get('username');

        BusinessMenu::create($_POST);

        return redirect('index');
    }

    public function search()
    {
        $info = $_POST['search'];

        $con['menu_name']= array('like',"%$info%");

        $data=BusinessMenu::where($con)->paginate(10);

        $this->assign('data',$data);

        return $this->fetch('index');
    }

    public function changeStatus($id)
    {
        $obj = BusinessMenu::where('id',$id)->find();

        $bool=$obj['menu_status']==1? 0 :1;

        BusinessMenu::where('id',$id)->update(['menu_status'=> $bool]);

        return true;
    }

    public function allChangeStatus()
    {
        $info=$_POST['arr'];

        foreach ($info as $v) {
            BusinessMenu::where('id',$v)->update(['menu_status'=>1]);
        }
        return true;
    }

    public function downChangeStatus()
    {
        $info=$_POST['arr'];

        foreach ($info as $v) {
            BusinessMenu::where('id',$v)->update(['menu_status'=>0]);
        }
        return true;
    }

    public function deleteChange()
    {
        $info=$_POST['arr'];

        foreach ($info as $v) {
            BusinessMenu::where('id',$v)->update(['menu_delete'=>0]);
        }
        return true;
    }

    public function delete($id)
    {
        BusinessMenu::where('id',$id)->update(['menu_delete'=>0]);
        return redirect('index');
    }

    public function edit($id)
    {
        $data = BusinessMenu::where('id',$id)->find();

        $this->assign('data',$data);

        $username = Session::get('username');

        $count = BusinessLogin::where('username',$username)->find();

        $num=$count['business_id'];

        $info = BusinessClassify::where('business_id',$num)->select();

        $this->assign('info',$info);

        $this->assign('id',$id);

        return $this->fetch();
    }

    public function update()
    {
        $file = request()->file("menu_img");

        $_POST['menu_classify'] = $_POST['classify_name'];

        unset($_POST['classify_name']);

        if($file){      //第一种是有图片输出的
            $info = $file->move(ROOT_PATH . 'public/uploads/shop');

            $nowDate=date('Ymd');

            $path=$nowDate.'/'.$info->getFilename();

            $_POST['menu_img'] = $path;

            $id=$_POST['id'];

            unset($_POST['id']);

            BusinessMenu::where('id',$id)->update($_POST);

            return redirect('index');
        }else{      //第二种是无图片输出的
            $id = $id=$_POST['id'];

            unset($_POST['id']);

            $info = BusinessMenu::where('id',$id)->find();

            $_POST['menu_img'] = $info['menu_img'];

            BusinessMenu::where('id',$id)->update($_POST);

            return redirect('index');
        }


    }
}